package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskTypeDAO;

import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskType entities
 * 
 */

@Service("TaskTypeService")

@Transactional
public class TaskTypeServiceImpl implements TaskTypeService {

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskType entities
	 * 
	 */
	@Autowired
	private TaskTypeDAO taskTypeDAO;

	/**
	 * Instantiates a new TaskTypeServiceImpl.
	 *
	 */
	public TaskTypeServiceImpl() {
	}

	/**
	 */
	@Transactional
	public TaskType findTaskTypeByPrimaryKey(Integer id) {
		return taskTypeDAO.findTaskTypeByPrimaryKey(id);
	}

	/**
	 * Return a count of all TaskType entity
	 * 
	 */
	@Transactional
	public Integer countTaskTypes() {
		return ((Long) taskTypeDAO.createQuerySingleResult("select count(o) from TaskType o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing TaskType entity
	 * 
	 */
	@Transactional
	public void saveTaskType(TaskType tasktype) {
		TaskType existingTaskType = taskTypeDAO.findTaskTypeByPrimaryKey(tasktype.getId());

		if (existingTaskType != null) {
			if (existingTaskType != tasktype) {
				existingTaskType.setId(tasktype.getId());
				existingTaskType.setName(tasktype.getName());
				existingTaskType.setMemo(tasktype.getMemo());
			}
			tasktype = taskTypeDAO.store(existingTaskType);
		} else {
			tasktype = taskTypeDAO.store(tasktype);
		}
		taskTypeDAO.flush();
	}

	/**
	 * Return all TaskType entity
	 * 
	 */
	@Transactional
	public List<TaskType> findAllTaskTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskType>(taskTypeDAO.findAllTaskTypes(startResult, maxRows));
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskType saveTaskTypeTasks(Integer id, Task related_tasks) {
		TaskType tasktype = taskTypeDAO.findTaskTypeByPrimaryKey(id, -1, -1);
		Task existingtasks = taskDAO.findTaskByPrimaryKey(related_tasks.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtasks != null) {
			existingtasks.setId(related_tasks.getId());
			existingtasks.setTaskNumber(related_tasks.getTaskNumber());
			existingtasks.setCreateTime(related_tasks.getCreateTime());
			existingtasks.setStartTime(related_tasks.getStartTime());
			existingtasks.setEndTime(related_tasks.getEndTime());
			existingtasks.setMemo(related_tasks.getMemo());
			related_tasks = existingtasks;
		}

		related_tasks.setTaskType(tasktype);
		tasktype.getTasks().add(related_tasks);
		related_tasks = taskDAO.store(related_tasks);
		taskDAO.flush();

		tasktype = taskTypeDAO.store(tasktype);
		taskTypeDAO.flush();

		return tasktype;
	}

	/**
	 * Load an existing TaskType entity
	 * 
	 */
	@Transactional
	public Set<TaskType> loadTaskTypes() {
		return taskTypeDAO.findAllTaskTypes();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskType deleteTaskTypeTasks(Integer tasktype_id, Integer related_tasks_id) {
		Task related_tasks = taskDAO.findTaskByPrimaryKey(related_tasks_id, -1, -1);

		TaskType tasktype = taskTypeDAO.findTaskTypeByPrimaryKey(tasktype_id, -1, -1);

		related_tasks.setTaskType(null);
		tasktype.getTasks().remove(related_tasks);

		taskDAO.remove(related_tasks);
		taskDAO.flush();

		return tasktype;
	}

	/**
	 * Delete an existing TaskType entity
	 * 
	 */
	@Transactional
	public void deleteTaskType(TaskType tasktype) {
		taskTypeDAO.remove(tasktype);
		taskTypeDAO.flush();
	}
}
